require_relative 'lib'
require_relative 'slot'
require_relative 'macrofunc'
require_relative 'scheduler'

$stdout.sync = true

def parse_enemy_step
  $lr = $stdin.gets.chomp.to_i

  case $lr
    when 1
      $card = $stdin.gets.chomp
      $slot = $stdin.gets.to_i
    when 2
      $slot = $stdin.gets.chomp.to_i
      $card = $stdin.gets.chomp

  end

  do_application($lr, $slot, $card)
end

def on_turn

  if !is_there_commands?
    rand(3) != 0 ? Scheduler.new.choose_and_heal  : Scheduler.new.choose_and_kill
  end

  $lr,$slot,$card = fetch_current_command
  #puts "#{$lr}, #{$slot}, #{$card}"
  do_application($lr, $slot, $card)

end

player_id = ARGV[0].to_i

parse_enemy_step if(player_id == 1)

step = 0
while true
  on_turn


  $stdout.puts $lr
  case $lr
    when 1
      $stdout.puts $card
      $stdout.puts $slot
    when 2
      $stdout.puts $slot
      $stdout.puts $card
  end

  #step+=1
  #if (step % 1000)==0
#    print "step=#{step}"
#    print_slots($my_slots, "my")
	  #print_slots($enemy_slots, "enemy")
    ##gets
  #end

  swap_slots
  parse_enemy_step
  swap_slots
end
